const router = require('express').Router()
const Query = require('./poolfun')
const {mySecret} = require('../functions/commonFun')

router.post('/register', (req, res) => {
    let {phone, password} = req.body
    let sql = `select id from users where name=${phone}`;
    Query(sql).then(response => {
        if (response.length > 0) {
            res.send({msg: `${phone} 已注册，请使用其他账号进行注册。`})
        }
        return {phone, password};
    }).then(({phone, password}) => {
        password = mySecret(password);
        let insertSql = `insert into users (name, password, login_time) values("${phone}","${password}","${new Date().getTime()}")`
        Query(insertSql).then(response => {
            if (response) {
                res.send({msg: '注册成功', status: 1})
            }
            res.send({msg: '注册失败', status: 0})
        }).catch(err => {
            res.send(err);
        })
    }).catch(err => {
        res.send(err);
    })
})

module.exports = router;
